package cqcvc.edu.cn.servlet;

import cqcvc.edu.cn.dao.MemberDao;
import cqcvc.edu.cn.entity.MemberInfo;
import cqcvc.edu.cn.entity.MemberLevel;
import org.apache.commons.fileupload.FileItem;
import org.apache.commons.fileupload.FileItemFactory;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.ServletFileUpload;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

@WebServlet(name = "RegisterActionServlet", urlPatterns = "/registerAction")
public class RegisterActionServlet extends HttpServlet {
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        request.setCharacterEncoding("utf8");
        response.setCharacterEncoding("utf8");
        MemberInfo memberInfo = new MemberInfo();
        byte[] buffer =null;
        //1 先判断上传的数据是否多段数据（只有是多段的数据，才是文件上传的）
        if (ServletFileUpload.isMultipartContent(request)) {
            FileItemFactory fileItemFactory = new DiskFileItemFactory();
            ServletFileUpload servletFileUpload = new ServletFileUpload(fileItemFactory);
            try {
                List<FileItem> list = servletFileUpload.parseRequest(request);
                for (FileItem fileItem : list) {
                    if (!fileItem.isFormField()) {
                        InputStream inputStream=fileItem.getInputStream();
                        buffer = new byte[(int)fileItem.getSize()];
                        ByteArrayOutputStream bos = new ByteArrayOutputStream();
                        byte[] b = new byte[4096];
                        int n;
                        while ((n = inputStream.read(b)) != -1) {
                            bos.write(b, 0, n);
                        }
                        inputStream.close();
                        bos.close();
                        buffer = bos.toByteArray();
                        memberInfo.setMemPic(buffer);
                    }
                    else{
                        if("memPassword".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemPassword(fileItem.getString("utf-8"));
                        }
                        if("memName".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemName(fileItem.getString("utf-8"));
                        }
                        if("memAge".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemAge(fileItem.getString("utf-8"));
                        }
                        if("memSex".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemSex(fileItem.getString("utf-8"));
                        }
                        if("memAddress".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemAddress(fileItem.getString("utf-8"));
                        }
                        if("memEmail".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemEmail(fileItem.getString("utf-8"));
                        }
                        if("memPhone".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemPhone(fileItem.getString("utf-8"));
                        }
                        if("memUserName".equals(fileItem.getFieldName()))
                        {
                            memberInfo.setMemUsername(fileItem.getString("utf-8"));
                        }
                    }
                }
            } catch (Exception e) {
                e.printStackTrace();
            }
            try {
                memberInfo.setMemberLevel(new MemberLevel(new Long(1)));
                memberInfo.setRegTime(new Date());
                memberInfo.setMemScore((float) 10);
                MemberDao md = new MemberDao();
                md.Insert(memberInfo);
                request.getSession().setAttribute("memberInfo", memberInfo);
                request.getRequestDispatcher("/pages/index/index.jsp").forward(request, response);

            } catch (Exception ex) {
                System.out.println(ex.getMessage());
            }
        }

    }

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }
}
